#!/bin/sh

#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright (C) 2002-2010 Mark Wong & Open Source Development Labs, Inc.
#               2006 Rilson Nascimento
#

USE_TABLESPACES=0
while getopts "d:p:t" OPT; do
	case ${OPT} in
	d)
		DBT5DBNAME=${OPTARG}
		;;
	p)
		PORT=${OPTARG}
		;;
	t)
		USE_TABLESPACES=1
		;;
	esac
done

if [ ! "x${DBT5DBNAME}" = "x" ]; then
	DBNAMEARG="-d ${DBT5DBNAME}"
fi

if [ ! "x${PORT}" = "x" ]; then
	PORTARG="-p ${PORT}"
fi

if [ ${USE_TABLESPACES} -eq 1 ]; then
	if [ -z ${DBT5TSDIR} ]; then
		echo "DBT5TSDIR not defined for tablespace path."
		exit 1
	fi
	TS_PK_ACCOUNT_PERMISSION_DIR="${DBT5TSDIR}/pk_account_permission/ts"
	TS_PK_CUSTOMER_DIR="${DBT5TSDIR}/pk_customer/ts"
	TS_PK_CUSTOMER_ACCOUNT_DIR="${DBT5TSDIR}/pk_customer_account/ts"
	TS_PK_CUSTOMER_TAXRATE_DIR="${DBT5TSDIR}/pk_customer_taxrate/ts"
	TS_PK_HOLDING_DIR="${DBT5TSDIR}/pk_holding/ts"
	TS_PK_HOLDING_HISTORY_DIR="${DBT5TSDIR}/pk_holding_history/ts"
	TS_PK_HOLDING_SUMMARY_DIR="${DBT5TSDIR}/pk_holding_summary/ts"
	TS_PK_WATCH_ITEM_DIR="${DBT5TSDIR}/pk_watch_item/ts"
	TS_PK_WATCH_LIST_DIR="${DBT5TSDIR}/pk_watch_list/ts"
	TS_PK_BROKER_DIR="${DBT5TSDIR}/pk_broker/ts"
	TS_PK_CASH_TRANSACTION_DIR="${DBT5TSDIR}/pk_cash_transaction/ts"
	TS_PK_CHARGE_DIR="${DBT5TSDIR}/pk_charge/ts"
	TS_PK_COMMISSION_RATE_DIR="${DBT5TSDIR}/pk_commission_rate/ts"
	TS_PK_SETTLEMENT_DIR="${DBT5TSDIR}/pk_settlement/ts"
	TS_PK_TRADE_DIR="${DBT5TSDIR}/pk_trade/ts"
	TS_PK_TRADE_HISTORY_DIR="${DBT5TSDIR}/pk_trade_history/ts"
	TS_PK_TRADE_REQUEST_DIR="${DBT5TSDIR}/pk_trade_request/ts"
	TS_PK_TRADE_TYPE_DIR="${DBT5TSDIR}/pk_trade_type/ts"
	TS_PK_COMPANY_DIR="${DBT5TSDIR}/pk_company/ts"
	TS_PK_COMPANY_COMPETITOR_DIR="${DBT5TSDIR}/pk_company_competitor/ts"
	TS_PK_DAILY_MARKET_DIR="${DBT5TSDIR}/pk_daily_market/ts"
	TS_PK_EXCHANGE_DIR="${DBT5TSDIR}/pk_exchange/ts"
	TS_PK_FINANCIAL_DIR="${DBT5TSDIR}/pk_financial/ts"
	TS_PK_INDUSTRY_DIR="${DBT5TSDIR}/pk_industry/ts"
	TS_PK_LAST_TRADE_DIR="${DBT5TSDIR}/pk_last_trade/ts"
	TS_PK_NEWS_ITEM_DIR="${DBT5TSDIR}/pk_news_item/ts"
	TS_PK_NEWS_XREF_DIR="${DBT5TSDIR}/pk_news_xref/ts"
	TS_PK_SECTOR_DIR="${DBT5TSDIR}/pk_sector/ts"
	TS_PK_SECURITY_DIR="${DBT5TSDIR}/pk_security/ts"
	TS_PK_ADDRESS_DIR="${DBT5TSDIR}/pk_address/ts"
	TS_PK_STATUS_TYPE_DIR="${DBT5TSDIR}/pk_status_type/ts"
	TS_PK_TAXRATE_DIR="${DBT5TSDIR}/pk_taxrate/ts"
	TS_PK_ZIP_CODE_DIR="${DBT5TSDIR}/pk_zip_code/ts"

	TS_I_C_TAX_ID_DIR="${DBT5TSDIR}/i_c_tax_id/ts"
	TS_I_CA_C_ID_DIR="${DBT5TSDIR}/i_ca_c_id/ts"
	TS_I_WL_C_ID_DIR="${DBT5TSDIR}/i_wl_c_id/ts"
	TS_I_DM_S_SYMB_DIR="${DBT5TSDIR}/i_dm_s_symb/ts"
	TS_I_TR_S_SYMB_DIR="${DBT5TSDIR}/i_tr_s_symb/ts"
	TS_I_T_ST_ID_DIR="${DBT5TSDIR}/i_t_st_id/ts"
	TS_I_T_ST_ID_DIR="${DBT5TSDIR}/i_t_st_id/ts"
	TS_I_T_CA_ID_DIR="${DBT5TSDIR}/i_t_ca_id/ts"
	TS_I_CO_NAME_DIR="${DBT5TSDIR}/i_co_name/ts"
	TS_I_SECURITY_DIR="${DBT5TSDIR}/i_security/ts"
	TS_I_HOLDING_DIR="${DBT5TSDIR}/i_holding/ts"
	TS_I_CHARGE_DIR="${DBT5TSDIR}/i_charge/ts"

	#
	# Creating 'ts' subdirectories because PostgreSQL doesn't like that
	# 'lost+found' directory if a device was mounted at
	# '${DBT5TSDIR}/warehouse'.
	#
	mkdir -p ${TS_PK_ACCOUNT_PERMISSION_DIR} || exit 1
	mkdir -p ${TS_PK_CUSTOMER_DIR} || exit 1
	mkdir -p ${TS_PK_CUSTOMER_ACCOUNT_DIR} || exit 1
	mkdir -p ${TS_PK_CUSTOMER_TAXRATE_DIR} || exit 1
	mkdir -p ${TS_PK_HOLDING_DIR} || exit 1
	mkdir -p ${TS_PK_HOLDING_HISTORY_DIR} || exit 1
	mkdir -p ${TS_PK_HOLDING_SUMMARY_DIR} || exit 1
	mkdir -p ${TS_PK_WATCH_ITEM_DIR} || exit 1
	mkdir -p ${TS_PK_WATCH_LIST_DIR} || exit 1
	mkdir -p ${TS_PK_BROKER_DIR} || exit 1
	mkdir -p ${TS_PK_CASH_TRANSACTION_DIR} || exit 1
	mkdir -p ${TS_PK_CHARGE_DIR} || exit 1
	mkdir -p ${TS_PK_COMMISSION_RATE_DIR} || exit 1
	mkdir -p ${TS_PK_SETTLEMENT_DIR} || exit 1
	mkdir -p ${TS_PK_TRADE_DIR} || exit 1
	mkdir -p ${TS_PK_TRADE_HISTORY_DIR} || exit 1
	mkdir -p ${TS_PK_TRADE_REQUEST_DIR} || exit 1
	mkdir -p ${TS_PK_TRADE_TYPE_DIR} || exit 1
	mkdir -p ${TS_PK_COMPANY_DIR} || exit 1
	mkdir -p ${TS_PK_COMPANY_COMPETITOR_DIR} || exit 1
	mkdir -p ${TS_PK_DAILY_MARKET_DIR} || exit 1
	mkdir -p ${TS_PK_EXCHANGE_DIR} || exit 1
	mkdir -p ${TS_PK_FINANCIAL_DIR} || exit 1
	mkdir -p ${TS_PK_INDUSTRY_DIR} || exit 1
	mkdir -p ${TS_PK_LAST_TRADE_DIR} || exit 1
	mkdir -p ${TS_PK_NEWS_ITEM_DIR} || exit 1
	mkdir -p ${TS_PK_NEWS_XREF_DIR} || exit 1
	mkdir -p ${TS_PK_SECTOR_DIR} || exit 1
	mkdir -p ${TS_PK_SECURITY_DIR} || exit 1
	mkdir -p ${TS_PK_ADDRESS_DIR} || exit 1
	mkdir -p ${TS_PK_STATUS_TYPE_DIR} || exit 1
	mkdir -p ${TS_PK_TAXRATE_DIR} || exit 1
	mkdir -p ${TS_PK_ZIP_CODE_DIR} || exit 1

	mkdir -p ${TS_I_C_TAX_ID_DIR} || exit 1
	mkdir -p ${TS_I_CA_C_ID_DIR} || exit 1
	mkdir -p ${TS_I_WL_C_ID_DIR} || exit 1
	mkdir -p ${TS_I_DM_S_SYMB_DIR} || exit 1
	mkdir -p ${TS_I_TR_S_SYMB_DIR} || exit 1
	mkdir -p ${TS_I_T_ST_ID_DIR} || exit 1
	mkdir -p ${TS_I_T_CA_ID_DIR} || exit 1
	mkdir -p ${TS_I_CO_NAME_DIR} || exit 1
	mkdir -p ${TS_I_SECURITY_DIR} || exit 1
	mkdir -p ${TS_I_HOLDING_DIR} || exit 1

	TS_PK_ACCOUNT_PERMISSION="TABLESPACE dbt5_pk_account_permission"
	TS_PK_CUSTOMER="TABLESPACE dbt5_pk_customer"
	TS_PK_CUSTOMER_ACCOUNT="TABLESPACE dbt5_pk_customer_account"
	TS_PK_CUSTOMER_TAXRATE="TABLESPACE dbt5_pk_customer_taxrate"
	TS_PK_HOLDING="TABLESPACE dbt5_pk_holding"
	TS_PK_HOLDING_HISTORY="TABLESPACE dbt5_pk_holding_history"
	TS_PK_HOLDING_SUMMARY="TABLESPACE dbt5_pk_holding_summary"
	TS_PK_WATCH_ITEM="TABLESPACE dbt5_pk_watch_item"
	TS_PK_WATCH_LIST="TABLESPACE dbt5_pk_watch_list"
	TS_PK_BROKER="TABLESPACE dbt5_pk_broker"
	TS_PK_CASH_TRANSACTION="TABLESPACE dbt5_pk_cash_transaction"
	TS_PK_CHARGE="TABLESPACE dbt5_pk_charge"
	TS_PK_COMMISSION_RATE="TABLESPACE dbt5_pk_commission_rate"
	TS_PK_SETTLEMENT="TABLESPACE dbt5_pk_settlement"
	TS_PK_TRADE="TABLESPACE dbt5_pk_trade"
	TS_PK_TRADE_HISTORY="TABLESPACE dbt5_pk_trade_history"
	TS_PK_TRADE_REQUEST="TABLESPACE dbt5_pk_trade_request"
	TS_PK_TRADE_TYPE="TABLESPACE dbt5_pk_trade_type"
	TS_PK_COMPANY="TABLESPACE dbt5_pk_company"
	TS_PK_COMPANY_COMPETITOR="TABLESPACE dbt5_pk_company_competitor"
	TS_PK_DAILY_MARKET="TABLESPACE dbt5_pk_daily_market"
	TS_PK_EXCHANGE="TABLESPACE dbt5_pk_exchange"
	TS_PK_FINANCIAL="TABLESPACE dbt5_pk_financial"
	TS_PK_INDUSTRY="TABLESPACE dbt5_pk_industry"
	TS_PK_LAST_TRADE="TABLESPACE dbt5_pk_last_trade"
	TS_PK_NEWS_ITEM="TABLESPACE dbt5_pk_news_item"
	TS_PK_NEWS_XREF="TABLESPACE dbt5_pk_news_xref"
	TS_PK_SECTOR="TABLESPACE dbt5_pk_sector"
	TS_PK_SECURITY="TABLESPACE dbt5_pk_security"
	TS_PK_ADDRESS="TABLESPACE dbt5_pk_address"
	TS_PK_STATUS_TYPE="TABLESPACE dbt5_pk_status_type"
	TS_PK_TAXRATE="TABLESPACE dbt5_pk_taxrate"
	TS_PK_ZIP_CODE="TABLESPACE dbt5_pk_zip_code"

	TS_I_C_TAX_ID="TABLESPACE dbt5_i_c_tax_id"
	TS_I_CA_C_ID="TABLESPACE dbt5_i_ca_c_id"
	TS_I_WL_C_ID="TABLESPACE dbt5_i_wl_c_id"
	TS_I_DM_S_SYMB="TABLESPACE dbt5_i_dm_s_symb"
	TS_I_TR_S_SYMB="TABLESPACE dbt5_i_tr_s_symb"
	TS_I_T_ST_ID="TABLESPACE dbt5_i_t_st_id"
	TS_I_T_CA_ID="TABLESPACE dbt5_i_t_ca_id"
	TS_I_CO_NAME="TABLESPACE dbt5_i_co_name"
	TS_I_SECURITY="TABLESPACE dbt5_i_security"
	TS_I_HOLDING="TABLESPACE dbt5_i_holding"
	TS_I_CHARGE="TABLESPACE dbt5_i_charge"

	#
	# Don't need to '|| exit 1' in case the tablespaces already exist.
	#
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_ACCOUNT_PERMISSION}
			    LOCATION '${TS_PK_ACCOUNT_PERMISSION_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_CUSTOMER} LOCATION '${TS_PK_CUSTOMER_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_CUSTOMER_ACCOUNT}
			    LOCATION '${TS_PK_CUSTOMER_ACCOUNT_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_CUSTOMER_TAXRATE}
			    LOCATION '${TS_PK_CUSTOMER_TAXRATE_DIR}';"
	psql ${PORTARG} -e -${DBNAMEARG} \
			-c "CREATE ${TS_PK_HOLDING} LOCATION '${TS_PK_HOLDING_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_HOLDING_HISTORY}
			    LOCATION '${TS_PK_HOLDING_HISTORY_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_HOLDING_SUMMARY}
			    LOCATION '${TS_PK_HOLDING_SUMMARY_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_WATCH_ITEM} LOCATION '${TS_PK_WATCH_ITEM_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_WATCH_LIST} LOCATION '${TS_PK_WATCH_LIST_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_BROKER} LOCATION '${TS_PK_BROKER_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_CASH_TRANSACTION}
			    LOCATION '${TS_PK_CASH_TRANSACTION_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_CHARGE} LOCATION '${TS_PK_CHARGE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_COMMISSION_RATE}
			    LOCATION '${TS_PK_COMMISSION_RATE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_SETTLEMENT} LOCATION '${TS_PK_SETTLEMENT_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_TRADE} LOCATION '${TS_PK_TRADE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_TRADE_HISTORY}
			    LOCATION '${TS_PK_TRADE_HISTORY_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_TRADE_REQUEST}
			    LOCATION '${TS_PK_TRADE_REQUEST_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_TRADE_TYPE} LOCATION '${TS_PK_TRADE_TYPE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_COMPANY} LOCATION '${TS_PK_COMPANY_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_COMPANY_COMPETITOR}
			    LOCATION '${TS_PK_COMPANY_COMPETITOR_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_DAILY_MARKET}
			    LOCATION '${TS_PK_DAILY_MARKET_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_EXCHANGE} LOCATION '${TS_PK_EXCHANGE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_FINANCIAL} LOCATION '${TS_PK_FINANCIAL_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_INDUSTRY} LOCATION '${TS_PK_INDUSTRY_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_LAST_TRADE} LOCATION '${TS_PK_LAST_TRADE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_NEWS_ITEM} LOCATION '${TS_PK_NEWS_ITEM_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_NEWS_XREF} LOCATION '${TS_PK_NEWS_XREF_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_SECTOR} LOCATION '${TS_PK_SECTOR_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_SECURITY} LOCATION '${TS_PK_SECURITY_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_ADDRESS} LOCATION '${TS_PK_ADDRESS_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_STATUS_TYPE}
			    LOCATION '${TS_PK_STATUS_TYPE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_TAXRATE} LOCATION '${TS_PK_TAXRATE_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_PK_ZIP_CODE} LOCATION '${TS_PK_ZIP_CODE_DIR}';"

	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_C_TAX_ID} LOCATION '${TS_I_C_TAX_ID_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_CA_C_ID} LOCATION '${TS_I_CA_C_ID_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_WL_C_ID} LOCATION '${TS_I_WL_C_ID_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_DM_S_SYMB} LOCATION '${TS_I_DM_S_SYMB_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_TR_S_SYMB} LOCATION '${TS_I_TR_S_SYMB_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_T_ST_ID} LOCATION '${TS_I_T_ST_ID_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_T_CA_ID} LOCATION '${TS_I_T_CA_ID_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_CO_NAME} LOCATION '${TS_I_CO_NAME_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_SECURITY} LOCATION '${TS_I_SECURITY_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_HOLDING} LOCATION '${TS_I_HOLDING_DIR}';"
	psql ${PORTARG} -e ${DBNAMEARG} \
			-c "CREATE ${TS_I_CHARGE} LOCATION '${TS_I_CHARGE_DIR}';"

	#
	# Rewrite these variables for the actualy index creation.
	#

	TS_PK_ACCOUNT_PERMISSION="USING INDEX ${TS_PK_ACCOUNT_PERMISSION}"
	TS_PK_CUSTOMER="USING INDEX ${TS_PK_CUSTOMER}"
	TS_PK_CUSTOMER_ACCOUNT="USING INDEX ${TS_PK_CUSTOMER_ACCOUNT}"
	TS_PK_CUSTOMER_TAXRATE="USING INDEX ${TS_PK_CUSTOMER_TAXRATE}"
	TS_PK_HOLDING="USING INDEX ${TS_PK_HOLDING}"
	TS_PK_HOLDING_HISTORY="USING INDEX ${TS_PK_HOLDING_HISTORY}"
	TS_PK_HOLDING_SUMMARY="USING INDEX ${TS_PK_HOLDING_SUMMARY}"
	TS_PK_WATCH_ITEM="USING INDEX ${TS_PK_WATCH_ITEM}"
	TS_PK_WATCH_LIST="USING INDEX ${TS_PK_WATCH_LIST}"
	TS_PK_BROKER="USING INDEX ${TS_PK_BROKER}"
	TS_PK_CASH_TRANSACTION="USING INDEX ${TS_PK_CASH_TRANSACTION}"
	TS_PK_CHARGE="USING INDEX ${TS_PK_CHARGE}"
	TS_PK_COMMISSION_RATE="USING INDEX ${TS_PK_COMMISSION_RATE}"
	TS_PK_SETTLEMENT="USING INDEX ${TS_PK_SETTLEMENT}"
	TS_PK_TRADE="USING INDEX ${TS_PK_TRADE}"
	TS_PK_TRADE_HISTORY="USING INDEX ${TS_PK_TRADE_HISTORY}"
	TS_PK_TRADE_REQUEST="USING INDEX ${TS_PK_TRADE_REQUEST}"
	TS_PK_TRADE_TYPE="USING INDEX ${TS_PK_TRADE_TYPE}"
	TS_PK_COMPANY="USING INDEX ${TS_PK_COMPANY}"
	TS_PK_COMPANY_COMPETITOR="USING INDEX ${TS_PK_COMPANY_COMPETITOR}"
	TS_PK_DAILY_MARKET="USING INDEX ${TS_PK_DAILY_MARKET}"
	TS_PK_EXCHANGE="USING INDEX ${TS_PK_EXCHANGE}"
	TS_PK_FINANCIAL="USING INDEX ${TS_PK_FINANCIAL}"
	TS_PK_INDUSTRY="USING INDEX ${TS_PK_INDUSTRY}"
	TS_PK_LAST_TRADE="USING INDEX ${TS_PK_LAST_TRADE}"
	TS_PK_NEWS_ITEM="USING INDEX ${TS_PK_NEWS_ITEM}"
	TS_PK_NEWS_XREF="USING INDEX ${TS_PK_NEWS_XREF}"
	TS_PK_SECTOR="USING INDEX ${TS_PK_SECTOR}"
	TS_PK_SECURITY="USING INDEX ${TS_PK_SECURITY}"
	TS_PK_ADDRESS="USING INDEX ${TS_PK_ADDRESS}"
	TS_PK_STATUS_TYPE="USING INDEX ${TS_PK_STATUS_TYPE}"
	TS_PK_TAXRATE="USING INDEX ${TS_PK_TAXRATE}"
	TS_PK_ZIP_CODE="USING INDEX ${TS_PK_ZIP_CODE}"

	TS_I_C_TAX_ID="USING INDEX ${TS_I_C_TAX_ID}"
	TS_I_CA_C_ID="USING INDEX ${TS_I_CA_C_ID}"
	TS_I_WL_C_ID="USING INDEX ${TS_I_WL_C_ID}"
	TS_I_DM_S_SYMB="USING INDEX ${TS_I_DM_S_SYMB}"
	TS_I_TR_S_SYMB="USING INDEX ${TS_I_TR_S_SYMB}"
	TS_I_T_ST_ID="USING INDEX ${TS_I_T_ST_ID}"
	TS_I_T_CA_ID="USING INDEX ${TS_I_T_CA_ID}"
	TS_I_CO_NAME="USING INDEX ${TS_I_CO_NAME}"
	TS_I_SECURITY="USING INDEX ${TS_I_SECURITY}"
	TS_I_HOLDING="USING INDEX ${TS_I_HOLDING}"
fi

pids=""

## PKs
#
## Clause 2.2.5.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE account_permission
#ADD CONSTRAINT pk_account_permission
#PRIMARY KEY (ap_ca_id, ap_tax_id)
#${TS_PK_ACCOUNT_PERMISSION};
#" & pids="$pids $!"
#
## Clause 2.2.5.2
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer
#ADD CONSTRAINT pk_customer
#PRIMARY KEY (c_id)
#${TS_PK_CUSTOMER};
#" & pids="$pids $!"
#
## Clause 2.2.5.3
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer_account
#ADD CONSTRAINT pk_customer_account
#PRIMARY KEY (ca_id)
#${TS_PK_CUSTOMER_ACCOUNT};
#" & pids="$pids $!"
#
## Clause 2.2.5.4
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer_taxrate
#ADD CONSTRAINT pk_customer_taxrate
#PRIMARY KEY (cx_tx_id, cx_c_id)
#${TS_PK_CUSTOMER_TAXRATE};
#" & pids="$pids $!"
#
## Clause 2.2.5.5
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding
#ADD CONSTRAINT pk_holding
#PRIMARY KEY (h_t_id)
#${TS_PK_HOLDING};
#" & pids="$pids $!"
#
## Clause 2.2.5.6
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding_history
#ADD CONSTRAINT pk_holding_history
#PRIMARY KEY (hh_h_t_id, hh_t_id)
#${TS_PK_HOLDING_HISTORY};
#" & pids="$pids $!"
#
## Clause 2.2.5.7
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding_summary
#ADD CONSTRAINT pk_holding_summary
#PRIMARY KEY (hs_ca_id, hs_s_symb)
#${TS_PK_HOLDING_SUMMARY};
#" & pids="$pids $!"
#
## Clause 2.2.5.8
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE watch_item
#ADD CONSTRAINT pk_watch_item 
#PRIMARY KEY (wi_wl_id, wi_s_symb)
#${TS_PK_WATCH_ITEM};
#" & pids="$pids $!"
#
## Clause 2.2.5.9
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE watch_list
#ADD CONSTRAINT pk_watch_list
#PRIMARY KEY (wl_id)
#${TS_PK_WATCH_LIST};
#" & pids="$pids $!"
#
## Clause 2.2.6.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE broker
#ADD CONSTRAINT pk_broker
#PRIMARY KEY (b_id)
#${TS_PK_BROKER};
#" & pids="$pids $!"
#
## Clause 2.2.6.2
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE cash_transaction
#ADD CONSTRAINT pk_cash_transaction
#PRIMARY KEY (ct_t_id)
#${TS_PK_CASH_TRANSACTION};
#" & pids="$pids $!"
#
## Clause 2.2.6.3
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE charge
#ADD CONSTRAINT pk_charge 
#PRIMARY KEY (ch_tt_id, ch_c_tier)
#${TS_PK_CHARGE};
#" & pids="$pids $!"
#
## Clause 2.2.6.4
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE commission_rate
#ADD CONSTRAINT pk_commission_rate 
#PRIMARY KEY (cr_c_tier, cr_tt_id, cr_ex_id, cr_from_qty)
#${TS_PK_COMMISSION_RATE};
#" & pids="$pids $!"
#
## Clause 2.2.6.5
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE settlement
#ADD CONSTRAINT pk_settlement PRIMARY KEY (se_t_id)
#${TS_PK_SETTLEMENT};
#" & pids="$pids $!"
#
## Clause 2.2.6.6
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade
#ADD CONSTRAINT pk_trade PRIMARY KEY (t_id)
#${TS_PK_TRADE};
#" & pids="$pids $!"
#
## Clause 2.2.6.7
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_history
#ADD CONSTRAINT pk_trade_history
#PRIMARY KEY (th_t_id, th_st_id)
#${TS_PK_TRADE_HISTORY};
#" & pids="$pids $!"
#
## Clause 2.2.6.8
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_request
#ADD CONSTRAINT pk_trade_request
#PRIMARY KEY (tr_t_id)
#${TS_PK_TRADE_REQUEST};
#" & pids="$pids $!"
#
## Clause 2.2.6.9
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_type
#ADD CONSTRAINT pk_trade_type
#PRIMARY KEY (tt_id)
#${TS_PK_TRADE_TYPE};
#" & pids="$pids $!"
#
## Clause 2.2.7.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company
#ADD CONSTRAINT pk_company
#PRIMARY KEY (co_id)
#${TS_PK_COMPANY};
#" & pids="$pids $!"
#
## Clause 2.2.7.2
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company_competitor
#ADD CONSTRAINT pk_company_competitor 
#PRIMARY KEY (cp_co_id, cp_comp_co_id, cp_in_id)
#${TS_PK_COMPANY_COMPETITOR};
#" & pids="$pids $!"
#
## Clause 2.2.7.3
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE daily_market
#ADD CONSTRAINT pk_daily_market
#PRIMARY KEY (dm_date, dm_s_symb)
#${TS_PK_DAILY_MARKET};
#" & pids="$pids $!"
#
## Clause 2.2.7.4
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE exchange
#ADD CONSTRAINT pk_exchange
#PRIMARY KEY (ex_id)
#${TS_PK_EXCHANGE};
#" & pids="$pids $!"
#
## Clause 2.2.7.5
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE financial
#ADD CONSTRAINT pk_financial
#PRIMARY KEY (fi_co_id, fi_year, fi_qtr)
#${TS_PK_FINANCIAL};
#" & pids="$pids $!"
#
## Clause 2.2.7.6
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE industry
#ADD CONSTRAINT pk_industry
#PRIMARY KEY (in_id)
#${TS_PK_INDUSTRY};
#" & pids="$pids $!"
#
## Clause 2.2.7.7
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE last_trade
#ADD CONSTRAINT pk_last_trade
#PRIMARY KEY (lt_s_symb)
#${TS_PK_LAST_TRADE};
#" & pids="$pids $!"
#
## Clause 2.2.7.8
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE news_item
#ADD CONSTRAINT pk_news_item
#PRIMARY KEY (ni_id)
#${TS_PK_NEWS_ITEM};
#" & pids="$pids $!"
#
## Clause 2.2.7.9
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE news_xref
#ADD CONSTRAINT pk_news_xref
#PRIMARY KEY (nx_ni_id, nx_co_id)
#${TS_PK_NEWS_XREF};
#" & pids="$pids $!"
#
## Clause 2.2.7.10
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE sector
#ADD CONSTRAINT pk_sector
#PRIMARY KEY (sc_id)
#${TS_PK_SECTOR};
#" & pids="$pids $!"
#
## Clause 2.2.7.11
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE security
#ADD CONSTRAINT pk_security
#PRIMARY KEY (s_symb)
#${TS_PK_SECURITY};
#" & pids="$pids $!"
#
## Clause 2.2.8.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE address
#ADD CONSTRAINT pk_address
#PRIMARY KEY (ad_id) ${TS_PK_ADDRESS};
#" & pids="$pids $!"
#
## Clause 2.2.8.2
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE status_type
#ADD CONSTRAINT pk_status_type
#PRIMARY KEY (st_id)
#${TS_PK_STATUS_TYPE};
#" & pids="$pids $!"
#
## Clause 2.2.8.3
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE taxrate
#ADD CONSTRAINT pk_taxrate
#PRIMARY KEY (tx_id)
#${TS_PK_TAXRATE};
#" & pids="$pids $!"
#
## Clause 2.2.8.4
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE zip_code
#ADD CONSTRAINT pk_zip_code
#PRIMARY KEY (zc_code)
#${TS_PK_ZIP_CODE};
#" & pids="$pids $!"

#sleep 1

# FKs
# The FKs of each table are stored in the same tablespace

# Clause 2.2.5.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE account_permission
#ADD CONSTRAINT fk_account_permission_ca FOREIGN KEY (ap_ca_id) 
#REFERENCES customer_account (ca_id);
#" & pids="$pids $!"
#
## Clause 2.2.5.2
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer
#ADD CONSTRAINT fk_customer_st FOREIGN KEY (c_st_id) 
#REFERENCES status_type (st_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer
#ADD CONSTRAINT fk_customer_ad FOREIGN KEY (c_ad_id) 
#REFERENCES address (ad_id);
#" & pids="$pids $!"
#
## Clause 2.2.5.3
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer_account
#ADD CONSTRAINT fk_customer_account_b FOREIGN KEY (ca_b_id) 
#REFERENCES broker (b_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer_account
#ADD CONSTRAINT fk_customer_account_c FOREIGN KEY (ca_c_id) 
#REFERENCES customer (c_id);
#" & pids="$pids $!"
#
## Clause 2.2.5.4
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer_taxrate
#ADD CONSTRAINT fk_customer_taxrate_tx FOREIGN KEY (cx_tx_id) 
#REFERENCES taxrate (tx_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE customer_taxrate
#ADD CONSTRAINT fk_customer_taxrate_c FOREIGN KEY (cx_c_id) 
#REFERENCES customer (c_id);
#" & pids="$pids $!"
#
## Clause 2.2.5.5
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding
#ADD CONSTRAINT fk_holding_t FOREIGN KEY (h_t_id) 
#REFERENCES trade (t_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding
#ADD CONSTRAINT fk_holding_hs FOREIGN KEY (h_ca_id, h_s_symb) 
#REFERENCES holding_summary (hs_ca_id, hs_s_symb);
#" & pids="$pids $!"
#
## Clause 2.2.5.6
## FIXME: Should the next two foreign keys be a single definition?
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding_history
#ADD CONSTRAINT fk_holding_history_t1 FOREIGN KEY (hh_h_t_id) 
#REFERENCES trade (t_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding_history
#ADD CONSTRAINT fk_holding_history_t2 FOREIGN KEY (hh_t_id) 
#REFERENCES trade (t_id);
#" & pids="$pids $!"
#
## Clause 2.2.5.7
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding_summary
#ADD CONSTRAINT fk_holding_summary_ca FOREIGN KEY (hs_ca_id) 
#REFERENCES customer_account (ca_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE holding_summary
#ADD CONSTRAINT fk_holding_summary_s FOREIGN KEY (hs_s_symb) 
#REFERENCES security (s_symb);
#" & pids="$pids $!"
#
## Clause 2.2.5.8
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE watch_item
#ADD CONSTRAINT fk_watch_item_wl FOREIGN KEY (wi_wl_id) 
#REFERENCES watch_list (wl_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE watch_item
#ADD CONSTRAINT fk_watch_item_s FOREIGN KEY (wi_s_symb) 
#REFERENCES security (s_symb);
#" & pids="$pids $!"
#
## Clause 2.2.5.9
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE watch_list
#ADD CONSTRAINT fk_watch_list FOREIGN KEY (wl_c_id) 
#REFERENCES customer (c_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE broker
#ADD CONSTRAINT fk_broker FOREIGN KEY (b_st_id) 
#REFERENCES status_type (st_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.2
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE cash_transaction
#ADD CONSTRAINT fk_cash_transaction FOREIGN KEY (ct_t_id) 
#REFERENCES trade (t_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.3
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE charge
#ADD CONSTRAINT fk_charge FOREIGN KEY (ch_tt_id) 
#REFERENCES trade_type (tt_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.4
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE commission_rate
#ADD CONSTRAINT fk_commission_rate_tt FOREIGN KEY (cr_tt_id) 
#REFERENCES trade_type (tt_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE commission_rate
#ADD CONSTRAINT fk_commission_rate_ex FOREIGN KEY (cr_ex_id) 
#REFERENCES exchange (ex_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.5
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE settlement
#ADD CONSTRAINT fk_settlement FOREIGN KEY (se_t_id) 
#REFERENCES trade (t_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.6
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade
#ADD CONSTRAINT fk_trade_st FOREIGN KEY (t_st_id) 
#REFERENCES status_type (st_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade
#ADD CONSTRAINT fk_trade_tt FOREIGN KEY (t_tt_id) 
#REFERENCES trade_type (tt_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade
#ADD CONSTRAINT fk_trade_s FOREIGN KEY (t_s_symb) 
#REFERENCES security (s_symb);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade
#ADD CONSTRAINT fk_trade_ca FOREIGN KEY (t_ca_id) 
#REFERENCES customer_account (ca_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.7
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_history
#ADD CONSTRAINT fk_trade_history_t FOREIGN KEY (th_t_id) 
#REFERENCES trade (t_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_history
#ADD CONSTRAINT fk_trade_history_st FOREIGN KEY (th_st_id) 
#REFERENCES status_type (st_id);
#" & pids="$pids $!"
#
## Clause 2.2.6.8
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_request
#ADD CONSTRAINT fk_trade_request_t FOREIGN KEY (tr_t_id) 
#REFERENCES trade (t_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_request
#ADD CONSTRAINT fk_trade_request_tt FOREIGN KEY (tr_tt_id) 
#REFERENCES trade_type (tt_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_request
#ADD CONSTRAINT fk_trade_request_s FOREIGN KEY (tr_s_symb) 
#REFERENCES security (s_symb);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE trade_request
#ADD CONSTRAINT fk_trade_request_b FOREIGN KEY (tr_b_id) 
#REFERENCES broker (b_id);
#" & pids="$pids $!"
#
## Clause 2.2.7.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company
#ADD CONSTRAINT fk_company_st FOREIGN KEY (co_st_id) 
#REFERENCES status_type (st_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company
#ADD CONSTRAINT fk_company_in FOREIGN KEY (co_in_id) 
#REFERENCES industry (in_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company
#ADD CONSTRAINT fk_company_ad FOREIGN KEY (co_ad_id) 
#REFERENCES address (ad_id);
#" & pids="$pids $!"
#
## Clause 2.2.7.2
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company_competitor
#ADD CONSTRAINT fk_company_competitor_co FOREIGN KEY (cp_co_id) 
#REFERENCES company (co_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company_competitor
#ADD CONSTRAINT fk_company_competitor_co2 FOREIGN KEY (cp_comp_co_id) 
#REFERENCES company (co_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE company_competitor
#ADD CONSTRAINT fk_company_competitor_in FOREIGN KEY (cp_in_id) 
#REFERENCES industry (in_id);
#" & pids="$pids $!"
#
## Clause 2.2.7.3
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE daily_market
#ADD CONSTRAINT fk_daily_market FOREIGN KEY (dm_s_symb) 
#REFERENCES security (s_symb);
#" & pids="$pids $!"
#
## Clause 2.2.7.4
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE exchange
#ADD CONSTRAINT fk_exchange FOREIGN KEY (ex_ad_id) 
#REFERENCES address (ad_id);
#" & pids="$pids $!"
#
## Clause 2.2.7.5
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE financial
#ADD CONSTRAINT fk_financial FOREIGN KEY (fi_co_id) 
#REFERENCES company (co_id);
#" & pids="$pids $!"
#
## Clause 2.2.7.6
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE industry
#ADD CONSTRAINT fk_industry FOREIGN KEY (in_sc_id) 
#REFERENCES sector (sc_id);
#" & pids="$pids $!"
#
## Clause 2.2.7.7
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE last_trade
#ADD CONSTRAINT fk_last_trade FOREIGN KEY (lt_s_symb) 
#REFERENCES security (s_symb);
#" & pids="$pids $!"
#
## Clause 2.2.7.9
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE news_xref
#ADD CONSTRAINT fk_news_xref_ni FOREIGN KEY (nx_ni_id) 
#REFERENCES news_item (ni_id);
#" & pids="$pids $!"
#
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE news_xref
#ADD CONSTRAINT fk_news_xref_co FOREIGN KEY (nx_co_id) 
#REFERENCES company (co_id);
#" & pids="$pids $!"
#
## Clause 2.2.7.11
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE security
#ADD CONSTRAINT fk_security_st FOREIGN KEY (s_st_id) 
#REFERENCES status_type (st_id);
#" & pids="$pids $!"

#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE security
#ADD CONSTRAINT fk_security_ex FOREIGN KEY (s_ex_id) 
#REFERENCES exchange (ex_id);
#" & pids="$pids $!"

#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE security
#ADD CONSTRAINT fk_security_co FOREIGN KEY (s_co_id) 
#REFERENCES company (co_id);
#" & pids="$pids $!"

## Clause 2.2.8.1
#psql ${PORTARG} -e ${DBNAMEARG} -c "
#ALTER TABLE address
#ADD CONSTRAINT fk_address FOREIGN KEY (ad_zc_code) 
#REFERENCES zip_code (zc_code);
#" & pids="$pids $!"

# Additional indexes

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_c_tax_id
ON customer (c_tax_id);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_ca_c_id
ON customer_account (ca_c_id);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_wl_c_id
ON watch_list (wl_c_id);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_dm_s_symb
ON daily_market (dm_s_symb);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_tr_s_symb
ON trade_request (tr_s_symb);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_t_st_id
ON trade (t_st_id);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_t_ca_id
ON trade (t_ca_id);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_t_s_symb
ON trade (t_s_symb);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_co_name
ON company (co_name);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_security
ON security (s_co_id, s_issue);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_holding
ON holding (h_ca_id, h_s_symb);
" & pids="$pids $!"

psql ${PORTARG} -e ${DBNAMEARG} -c "
CREATE INDEX i_hh_t_id
ON holding_history (hh_t_id);
" & pids="$pids $!"

wait ${pids}
